'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	if (event.leixing == 0) {
		let result = await uniCloud.deleteFile({
			fileList: event.picurl,
			success(res) {},
			fail(err) {}
		});
		return result
	} else if (event.leixing == 1) { //修改轮播图
		let result = await db.collection('banner').where({
			_id: event.id
		}).update({
			url: event.url,
			biaoti: event.biaoti,
			lianjie: event.lianjie,
			time: event.time
		})
		return result
	} else if (event.leixing == 2) { //提交轮播图
		let result = await db.collection('banner').add({
			url: event.url,
			biaoti: event.biaoti,
			lianjie: event.lianjie,
			time: event.time
		})
		return result
	} else if (event.leixing == 3) {
		const res = await db.collection('banner').get()
		return {
			res
		}
	} else if (event.leixing == 4) {
		const res = await db.collection('banner').where({
			_id: event.id
		}).remove()
	} else if (event.leixing == 5) { //修改教务老师
		const res = await db.collection('uni-id-users').where({
			_id: event.id
		}).update({
			name: event.name,
			mobile: event.tel,
			avatar: event.zhaopian,
			role: event.role,
			jingshouren: event.jingshouren
		})
		let res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '修改老师数据'
		})
	} else if (event.leixing == 6) { //添加老师信息
		let result = await db.collection('uni-id-users').add(event.users)
		let res = await db.collection('xiugaishuju').add(event.xiugai_shuju)
		return res
	} else if (event.leixing == 7) { //删除老师信息
		let result = await db.collection('uni-id-users').where({
			_id: event.id
		}).remove()
		let res = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '删除老师数据'
		})
		return result
	} else if (event.leixing == 8) {
		let result = await db.collection('kecheng').where({
			_id: event.id
		}).remove()
		let res = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '删除课程数据',
			xiaoqu: event.xiaoqu
		})
		return result
	} else if (event.leixing == 9) {
		const res = db.collection('goods_fenlei')
			.aggregate()
			// 阶段1：优先筛选主表
			.match({ weizhi: db.command.in(["1"]) })
			// 阶段2：关联子表
			.lookup({
				from: 'goods',
				localField: '_id',
				foreignField: 'fenlei1_id',
				as: 'fenlei_info',
			})
			.end();
		return res
	} else if (event.leixing == 10) { //
		const res = await db.collection('baokelist').doc(event.id).remove()
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '删除报课记录',
			xiaoqu: event.xiaoqu
		})
		const res2 = await db.collection('zhangwuliushui').add({
			jine: event.jine,
			biandong: '支出',
			leibie: '删除报课记录',
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu
		})
		return res
	} else if (event.leixing == 11) { //
		const res = await db.collection('baokelist').doc(event.id).update({
			isxufei: 0
		})
		return res
	} else if (event.leixing == 12) { //类型为10时，获取教务老师数据
		const dbJQL = uniCloud.databaseForJQL({
			event,
			context
		})
		dbJQL.setUser({ // 指定后续执行操作的用户信息，此虚拟用户将同时拥有传入的uid、role、permission
			// uid: 'user-id', // 用户id
			role: ['admin'], // 指定当前执行用户的角色为admin。如果只希望指定为admin身份，可以删除uid和permission字段
			// permission: [] // 用户权限
		})
		const res = await dbJQL.collection('uni-id-users').where(event.tiaojian).field('mobile,name,avatar,my_invite_code,tuijian_lian').get()
		return res
	}else if(event.leixing==13){
		const res= await db.collection('fenxianghaibao').get()
		return res
	}else if(event.leixing==14){
		const res= await db.collection('fenxianghaibao').add(event.shuju)
		return res
	}else if(event.leixing==15){
		const res= await db.collection('fenxianghaibao').where(event.tiaojian).update(event.shuju)
		return res
	}else if(event.leixing==16){
		const res= await db.collection('fenxianghaibao'). where(event.tiaojian).remove()
		return res
	}else if(event.leixing==17){
		const res= await db.collection('cidian').where(event.shuju).get()
		return res
	}else if(event.leixing==18){
		const res= await db.collection('cidian').where(event.tiaojian).update(event.shuju)
		return res
	}
};